<?php
class STE_Video extends STE_Media
{
	public function getVideoCountByAlbumType($albumID)
	{
		$r = $this->_fetchStatement("CALL sp_admin_get_video_count(?)", array($albumID));
		return empty($r) ? null : $r[0]; 
	}
	
	public function getVideo($videoID)
	{
		$r = $this->_fetchStatement("CALL sp_admin_get_video(?)", array($videoID));
		return empty($r) ? null : $r[0];
	}

	public function getVideoList($albumID, $from, $numberOfVideos)
	{
		return $this->_fetchStatement("CALL sp_admin_get_video_list(?, ?, ?)", array($albumID, $from, $numberOfVideos));
	}
	
	public function insertVideo($albumID, $fileName, $videoDescription)
	{		
		$successful = $this->_moveTempVideoToMediaFolder($fileName);
		if(!$successful) {
			return false;
		}
		
		return $this->_executeStatement("CALL sp_admin_insert_video(?, ?, ?)", 
										array(
											$albumID,
											self::MEDIA_URL_PREFIX . $fileName,
											$videoDescription));
	}
	
	private function _moveTempVideoToMediaFolder($fileName)
	{			
		return rename(self::MEDIA_TEMP_DIRECTORY . $fileName, self::MEDIA_DIRECTORY . $fileName);		
	}
	
	public function updateVideo($videoID, $videoDescription)
	{
		return $this->_executeStatement("CALL sp_admin_update_video(?, ?)", 
										array($videoID,
											  $videoDescription));
	}	
	
	public function deleteVideo($videoID)
	{	
		// get media row before delete it
		$video = $this->getVideo($videoID);
	
		// Delete media file from server.		
		$successful = $this->_deleteMediaFile($this->_getFileNameFromFilePath($video['path']));		
		if(!$successful) {
			return false;
		}
		return $this->_executeStatement("CALL sp_admin_delete_video(?)", 
										array($videoID));	
	}
}	
?>